package common 
{
	import debug.ASSERT;
	/**
	 * ...
	 * @author anod
	 */
	public class StructTime implements IData
	{
		private static const MINUTES_PER_HOUR:int = 60;
		
		private var mID:int;					//班次id
		private var mName:String;				//班次名称
		private var mStart:int;					//班次开始时间（相对于00:00经过的分钟数）
		private var mSpend:int;					//班次持续时间（单位分钟）
		private var mSize:int;					//班次人数
		
		public function StructTime( id:int ) 
		{
			super();
			mID = id;
		}
		
		/* INTERFACE common.IData */
		
		public function loadFromXML(xml:XML):void 
		{
			ASSERT( xml != null, "加载的xml为空" );
			//<item name="xxoo" start="100" spend="120" size="6"/>
			mName  = String(xml.@name);
			mStart = int(xml.@start);
			mSpend = int(xml.@spend);
			mSize  = int(xml.@size);
		}
		
		public function toXML():String 
		{
			return '<item name="' + mName + '" start="' + mStart + '" spend="' + mSpend + '" size="' + mSize + '"/>';
		}
		
		public function get ID():int 
		{
			return mID;
		}
		
		public function get name():String 
		{
			return mName;
		}
		
		public function set name(value:String):void 
		{
			ASSERT( value != null, "班次名称不能为空" );
			mName = value;
		}
		
		public function get start():int 
		{
			return mStart;
		}
		
		public function set start(value:int):void 
		{
			ASSERT( value >= 0, "开始时间必须为非负整数" );
			mStart = value;
		}
		
		public function get spend():int 
		{
			return mSpend;
		}
		
		public function set spend(value:int):void 
		{
			ASSERT( value >= 0, "班次时长必须为非负整数" );
			mSpend = value;
		}
		
		public function get size():int 
		{
			return mSize;
		}
		
		public function set size(value:int):void 
		{
			mSize = value;
		}
		
		public function set startHour( v:int ):void
		{
			ASSERT( v >= 0, "开始时间必须为非负整数");
			mStart = v * MINUTES_PER_HOUR + startMinute;
		}
		public function get startHour():int
		{
			return mStart / MINUTES_PER_HOUR;
		}
		public function set startMinute( v:int ):void
		{
			ASSERT( v >= 0, "开始时间必须为非负整数");
			mStart = startHour * MINUTES_PER_HOUR + v;
		}
		public function get startMinute():int
		{
			return mStart % MINUTES_PER_HOUR;
		}
		public function setStartTime( hour:int, minute:int ):void
		{
			ASSERT( hour >= 0 && minute >= 0, "开始时间必须为非负整数");
			mStart = hour * MINUTES_PER_HOUR + minute;
		}
		
		public function set spendHours( v:int ):void
		{
			ASSERT( v >= 0, "班次时长必须为非负整数");
			mSpend = v * MINUTES_PER_HOUR + spendMinutes;
		}
		public function get spendHours():int
		{
			return mSpend / MINUTES_PER_HOUR;
		}
		public function set spendMinutes( v:int ):void
		{
			ASSERT( v >= 0, "班次时长必须为非负整数");
			mSpend = spendHours * MINUTES_PER_HOUR + v;
		}
		public function get spendMinutes():int
		{
			return mSpend % MINUTES_PER_HOUR;
		}
		public function setSpendTime( hour:int, minute:int ):void
		{
			ASSERT( hour >= 0 && minute >= 0, "班次时长必须为非负整数");
			mSpend = hour * MINUTES_PER_HOUR + minute;
		}
		
		public function set endHour( v:int ):void
		{
			ASSERT( v >= 0, "结束时间必须为非负整数");
			ASSERT( v >= startHour, "结束时间必须在开始时间之后");
			spendHours = endHour - startHour;
		}
		public function get endHour():int
		{
			return (mStart + mSpend) / MINUTES_PER_HOUR;
		}
		public function set endMinute( v:int ):void
		{
			ASSERT( v >= 0, "结束时间必须为非负整数");
			ASSERT( v >= startMinute, "结束时间必须在开始时间之后");
			spendMinutes = endMinute - startMinute;
		}
		public function get endMinute():int
		{
			return (mStart + mSpend) % MINUTES_PER_HOUR;
		}
		public function setEndTime( hour:int, minute:int ):void
		{
			var end:int = hour * MINUTES_PER_HOUR + minute;
			ASSERT( end > mStart, "结束时间必须在开始时间之后" );
			spend = end - mStart;
		}
	}
}